package com.dykj.fireFighting.web.base.analysis.controller;

import com.dykj.fireFighting.api.base.analysis.dto.DevHisAnalysisDto;
import com.dykj.fireFighting.api.base.analysis.dto.QueryAnalysisDto;
import com.dykj.fireFighting.api.base.analysis.entity.FireAlarmAnalysisResult;
import com.dykj.fireFighting.api.base.devHisResult.entity.TongBiBean;
import com.dykj.fireFighting.api.base.devHisResult.service.IDevHisResultService;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.maintenance.service.IUrgentMaintenanceService;
import com.dykj.fireFighting.api.elasticsearch.devHis.entity.EsDevHis;
import com.dykj.fireFighting.api.elasticsearch.devHis.service.EsDevHisService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.DateTimeCutUtil;
import com.dykj.fireFighting.common.core.utils.DateUtil;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zly
 * @Description :消防数据页 火警自动报警系统  故障分析
 * @Date: 2022-04-14
 */
@RestController
@RequestMapping("fireFaultAlarmAnalysis")
public class FireAlarmAnalysisController {

    @DubboReference
    private IDevHisResultService devHisResultService;
    @DubboReference
    private IEnterpriseService enterpriseService;
    @DubboReference
    private IUrgentMaintenanceService urgentMaintenanceService;
    @DubboReference
    private EsDevHisService esDevHisService;
    /**
     *   查询今日火警数量必须传入当天日期   查询火警总数不加时间  使用字段 fireNum  faultNum
     * @param queryAnalysis
     * @return
     */
    @AutoLog(value = "今日火警/故障、火警/故障总数")
    @ApiOperation(value = "今日火警/故障、火警/故障总数", notes = "今日火警/故障、火警/故障总数")
    @GetMapping("/getFireNum")
    public CommonResult getFireNum(QueryAnalysisDto queryAnalysis){

        LoginUserVo logUser = ShiroUtil.getUser();
        //queryAnalysis.setCenterUnitId(logUser.getEnterpriseId().toString());
        List<Enterprise> enterprises = enterpriseService.queryUnitSelect(logUser.getEnterpriseId().toString());
        queryAnalysis.setSocialUnitList(enterprises);

        FireAlarmAnalysisResult result=devHisResultService.getFireNum(queryAnalysis);
        return CommonResult.success(result,"查询成功");
    }


    @AutoLog(value = "火灾自动报警系统-社会单位火灾自动报警/故障数对比")
    @ApiOperation(value = "火灾自动报警系统-社会单位火灾自动报警/故障数对比", notes = "火灾自动报警系统-社会单位火灾自动报警/故障数对比")
    @GetMapping("/getFireChartData")
    public CommonResult getFireChartData(QueryAnalysisDto queryAnalysis) throws ParseException {

        LoginUserVo logUser = ShiroUtil.getUser();
        queryAnalysis.setCenterUnitId(logUser.getEnterpriseId().toString());
        List<Enterprise> enterprises = enterpriseService.queryUnitSelect(logUser.getEnterpriseId().toString());
        queryAnalysis.setSocialUnitList(enterprises);

        List<FireAlarmAnalysisResult> list=devHisResultService.getFireChartData(queryAnalysis);
        /**
         * type取值1 年，2 季度 ，3 月，4 日
         * 补零
         */
        List<String> resDateNameList = list.stream().map(WarningRecovery -> {
            return WarningRecovery.getDateDisplay() ;
        }).collect(Collectors.toList());
        List<String> newList = new ArrayList<>();

        switch (queryAnalysis.getType()){
            case 1:
                List<String> yearList = DateTimeCutUtil.cutByType(queryAnalysis.getBeginTime(), queryAnalysis.getEndTime(), "year");
                newList=DateTimeCutUtil.subList2(yearList,resDateNameList);
                break;
            case 2:
                List<String> quarterList = DateTimeCutUtil.cutByType(queryAnalysis.getBeginTime(), queryAnalysis.getEndTime(), "quarter");
                newList=DateTimeCutUtil.subList2(quarterList,resDateNameList);
                break;
            case 3:
                List<String>  monthList = DateTimeCutUtil.cutByType(queryAnalysis.getBeginTime(), queryAnalysis.getEndTime(), "month");
                newList=DateTimeCutUtil.subList2(monthList,resDateNameList);
                break;
            case 4:
                List<String> dayList = DateTimeCutUtil.cutByType(queryAnalysis.getBeginTime(), queryAnalysis.getEndTime(), "day");
                newList=DateTimeCutUtil.subList2(dayList,resDateNameList);
                break;
        }
        ArrayList<FireAlarmAnalysisResult> addList = new ArrayList<>();
        for (int i = 0; i <newList.size() ; i++) {
            FireAlarmAnalysisResult f = new FireAlarmAnalysisResult();
            f.setDateDisplay(newList.get(i));
            f.setFireNum(0);
            f.setFaultNum(0);
            addList.add(f);
        }
        list.addAll(addList);
       list=list.stream().sorted(Comparator.comparing(FireAlarmAnalysisResult::getDateDisplay)).collect(Collectors.toList());
        return CommonResult.success(list,"查询成功");
    }

    @AutoLog(value = "火灾自动报警系统-各区域社会单位故障数")
    @ApiOperation(value = "火灾自动报警系统-各区域社会单位故障数", notes = "火灾自动报警系统-各区域社会单位故障数")
    @GetMapping("/getFaultChartData")
    public CommonResult getFaultChartData(QueryAnalysisDto queryAnalysis){

        LoginUserVo logUser = ShiroUtil.getUser();
        queryAnalysis.setCenterUnitId(logUser.getEnterpriseId().toString());
        List<Enterprise> enterprises = enterpriseService.queryUnitSelect(logUser.getEnterpriseId().toString());
        queryAnalysis.setSocialUnitList(enterprises);

        List<FireAlarmAnalysisResult> list=devHisResultService.getFaultChartData(queryAnalysis);
        return CommonResult.success(list,"查询成功");
    }

    /**
     *   查询今日隐患数量必须传入当天日期   查询隐患总数不加时间
     * @param queryAnalysis
     * @return
     */
    @AutoLog(value = "故障分析-隐患数量、隐患总数")
    @ApiOperation(value = "故障分析-今日隐患数、隐患总数", notes = "故障分析-今日隐患数、隐患总数")
    @GetMapping("/getFaultNum")
    public CommonResult getFaultNum(QueryAnalysisDto queryAnalysis){
        LoginUserVo logUser = ShiroUtil.getUser();
        queryAnalysis.setCenterUnitId(logUser.getEnterpriseId().toString());
        List<Enterprise> enterprises = enterpriseService.queryUnitSelect(logUser.getEnterpriseId().toString());
        queryAnalysis.setSocialUnitList(enterprises);

        List<String> ids = new ArrayList<>();
        for (Enterprise list : enterprises ) {
            ids.add(list.getId() + "");
        }
        int faultNum = urgentMaintenanceService.getCenterNum(ids, queryAnalysis.getBeginTime(), queryAnalysis.getEndTime());
        return CommonResult.success(faultNum,"查询成功");
    }

    @AutoLog(value = "故障分析-列表")
    @ApiOperation(value = "故障分析-列表", notes = "故障分析-列表")
    @GetMapping("/getFaultDangerList")
    public CommonResult<PageVo<FireAlarmAnalysisResult>> getFaultDangerList(QueryAnalysisDto queryAnalysis){
        LoginUserVo logUser = ShiroUtil.getUser();
        queryAnalysis.setCenterUnitId(logUser.getEnterpriseId().toString());
        List<Enterprise> enterprisesList = new ArrayList<Enterprise>();
        if(queryAnalysis.getUnitId() !=null && StringUtils.isNotBlank(queryAnalysis.getUnitId())){
            Enterprise enterprise = new Enterprise();
            enterprise.setId(Long.valueOf(queryAnalysis.getUnitId()));
            enterprisesList.add(enterprise);
            queryAnalysis.setSocialUnitList(enterprisesList);

        }else{
            enterprisesList = enterpriseService.queryUnitSelect(logUser.getEnterpriseId().toString());
            queryAnalysis.setSocialUnitList(enterprisesList);
        }


        PageVo<FireAlarmAnalysisResult> pageVo=devHisResultService.getFaultDangerList(queryAnalysis);
        return CommonResult.success(pageVo,"查询成功");
    }

    @AutoLog(value = "故障分析-社会单位故障数量（排名榜）")
    @ApiOperation(value = "故障分析-社会单位故障数量（排名榜）", notes = "故障分析-社会单位故障数量（排名榜）")
    @GetMapping("/getFaultRankingList")
    public CommonResult getFaultRankingList(QueryAnalysisDto queryAnalysis){
        LoginUserVo logUser = ShiroUtil.getUser();
        //queryAnalysis.setCenterUnitId(logUser.getEnterpriseId().toString());
        if(null == queryAnalysis.getBeginTime() || null == queryAnalysis.getEndTime()){
            queryAnalysis.setBeginTime(DateUtil.getMonthFirstDay());
            queryAnalysis.setEndTime(DateUtil.getMonthLastDay());
        }
        List<Enterprise> enterprises = enterpriseService.queryUnitSelect(logUser.getEnterpriseId().toString());
        queryAnalysis.setSocialUnitList(enterprises);

        if(queryAnalysis.getBeginIndex() !=null ){
            queryAnalysis.setBeginIndex(queryAnalysis.getBeginIndex()-1);
        }else{
            queryAnalysis.setBeginIndex(0);
        }
       List<FireAlarmAnalysisResult> list=devHisResultService.getFaultRankingList(queryAnalysis);
        return CommonResult.success(list,"查询成功");
    }

    @AutoLog(value = "同比环比数据")
    @ApiOperation(value = "同比环比数据", notes = "同比环比数据")
    @GetMapping("/getTongBiByMonth")
    public CommonResult<List<TongBiBean>> getTongBiByMonth(QueryAnalysisDto queryAnalysisDto) {
        Long enterpriseId = ShiroUtil.getUser().getEnterpriseId();
        List<TongBiBean> tongBiBeanList =devHisResultService.getTongBiByMonth(queryAnalysisDto.getBiliType(),queryAnalysisDto.getEndTime(),queryAnalysisDto.getBeginTime(),enterpriseId,queryAnalysisDto.getUnitId());
        return CommonResult.success(tongBiBeanList);
    }



    }
